﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Yitter.IdGenerator;

namespace SheepCommon.API.Vendors
{
    public class BankaccountCreateHander : IRequestHandler<BankaccountCreateCommand, Result>
    {
        private readonly IBaseRepository<Bankaccount> _bankaccountRepository;
        private readonly IMapper _mapper;
        public BankaccountCreateHander(IBaseRepository<Bankaccount> bankaccountRepository, IMapper mapper)
        {
            _bankaccountRepository = bankaccountRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 创建银行账户
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(BankaccountCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                var bankaccount = _mapper.Map<Bankaccount>(request);
                var list = await _bankaccountRepository.Add(bankaccount);
                if (list > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;
        }
    }
}
